package com.vf.admin.utils;

import com.vf.admin.client.service.AppEarningsService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.miaodong.entity.AppEarnings;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AccountUtil.java
 * @Description 账户操作工具类
 * @createTime 2021年07月24日 14:36:00
 */
public class AccountUtil {

    private static AccountUtil Instance = new AccountUtil();

    private AccountUtil() {
    }

    public static AccountUtil getInstance() {
        return Instance;
    }

    @Autowired
    AppUserService appUserService;
    @Autowired
    static AppUserService staticappUserService;
    @Autowired
    AppEarningsService appEarningsService;
    @Autowired
    static AppEarningsService staticappEarningsService;

    Lock lock = new ReentrantLock();

    @PostConstruct
    public void init() {
        staticappUserService = appUserService;
        staticappEarningsService = appEarningsService;
    }
    //用户钻石
    public void addZuanShi(Integer userid, BigDecimal num) {
        lock.lock();
        try {
            staticappUserService.updateZuanShi(userid, num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    //用户魅力值
    public void addMeiLi(Integer userid, Integer num) {
        lock.lock();
        try {
            staticappUserService.updatemeili(userid, num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    //用户金豆     zengyi 1加 2减少
    public void addJinBi(Integer userid, Integer num, Integer zengyi, String neirong) {
        lock.lock();
        try {
            //更新金币
            staticappUserService.addJindou(userid, new BigDecimal(num));
            //添加明细
            AppEarnings haoYou = new AppEarnings();
            haoYou.setNeirong(neirong);
            haoYou.setShouyi(zengyi);
            haoYou.setMoney(Double.valueOf(num));
            haoYou.setType(4);//1充值 2提现  3兑换 4金币  5收礼  6送礼
            haoYou.setUserid(userid);
            haoYou.setCreatetime(new Date());
            staticappEarningsService.save(haoYou);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}